<?php

namespace app\api\controller;

use app\common\controller\UnLogin;
use app\common\service\MarketService;
use think\Db;
//行情
class Market extends UnLogin{

	public function getMarket(){
		$data = cache('hangqing');
		if(!$data){
			$data = MarketService::getMarket();
		}
		if ($data) {
			$this->retData(0, 'success', $data);
		} else {
			$this->retData(0, 'fail');
		}
	}

	public function Kline(){
		$name = P('name')?:'eth';
		if (empty($name)) {
			$this->retData(1, '缺少参数');
		}
		$market_name = strtolower($name).'_usdt';
		$data = curl('https://data.gateio.co/api2/1/candlestick2/'.$market_name.'?group_set=60&range_hour=50');
		$data = json_decode($data,1);
		$d = [];
		if (!isset($data['code'])) {
			
			foreach ($data['data'] as $key => $val) {
				$single = [];
				$single[] = $val[0]/1;
				$single[] = (float)$val[2];
				$d[] = $single;
			}		
			$this->retData(0, 'success', $d);
		} else {
			$coin = model('CoinModel')->getInfoByName(strtoupper($name));
			$data = Db::table('tp_market')->where('cid',$coin['id'])->select();
			if($data){
				foreach ($data as $key => $val) {
					$single = [];
					$single[] = $val['date']*1000;
					$single[] = (float)$val['price'];
					$d[] = $single;
				}
				$this->retData(0, 'success', $d);
			} else {
				$this->retData(0, 'fail');
			}
		}
	}
}